package com.example.dao.impl;

import com.example.dao.CustomerAdminDao;
import com.example.entity.IndexInfo;
import com.example.entity.customerAdmin.ContactUser;
import com.example.entity.customerAdmin.Custom;
import com.example.entity.customerAdmin.CustomerAdmin;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerAdminDaoImpl implements CustomerAdminDao {
    @Override
    public int add (CustomerAdmin customerAdmin) {
        int i=-1;
        try {
            Object[] objects=new Object[7];
            objects[0]=customerAdmin.getName ();
            objects[1]=customerAdmin.getStarRating ();
            objects[2]=customerAdmin.getSaleUser ();
            objects[3]=customerAdmin.getPhone ();
            objects[4]=customerAdmin.getFollowThough ();
            objects[5]=customerAdmin.getNoFollow ();
            objects[6]=customerAdmin.getStates ();
            String sql="insert into customer_admin(name,star_rating,sale_user,phone,follow_through,no_follow,states) value(?,?,?,?,?,?,?)";
           i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int delete (int id) {
        int i=-1;
        try {
            Object[] objects=new Object[1];
            objects[0]=id;
           i= BaseDao.modify ("delete from customer_admin where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (CustomerAdmin customerAdmin) {
        int i=-1;
        try {
            Object[] objects=new Object[8];
            objects[0]=customerAdmin.getName ();
            objects[1]=customerAdmin.getStarRating ();
            objects[2]=customerAdmin.getSaleUser ();
            objects[3]=customerAdmin.getPhone ();
            objects[4]=customerAdmin.getFollowThough ();
            objects[5]=customerAdmin.getNoFollow ();
            objects[6]=customerAdmin.getStates ();
            objects[7]=customerAdmin.getId ();
            String sql="update customer_admin set name=?,star_rating=?,sale_user=?,phone=?,follow_through=?,no_follow=?,states=? where id=?";
           i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<CustomerAdmin> selectAll (int page, int limit) {
        List<CustomerAdmin> customerAdmins=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from customer_admin limit ?,?", objects);
        try {
            while (select.next ()){
                CustomerAdmin customerAdmin=new CustomerAdmin();
                customerAdmin.setId (select.getInt ("id"));
                customerAdmin.setName (select.getString ("name"));
                customerAdmin.setStarRating (select.getString ("star_rating"));
                customerAdmin.setPhone (select.getString ("phone"));
                customerAdmin.setSaleUser (select.getString ("sale_user"));
                customerAdmin.setFollowThough (select.getString ("follow_through"));
                customerAdmin.setNoFollow (select.getInt ("no_follow"));
                customerAdmin.setStates (select.getString ("states"));
                customerAdmin.setType(select.getInt("type"));
                customerAdmins.add (customerAdmin);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return customerAdmins;
    }

    @Override
    public List<CustomerAdmin> selectById (int id) {
        List<CustomerAdmin>customerAdminList=new ArrayList<>();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from customer_admin where id=? ", objects);
        try {
            while (select.next ()){
                CustomerAdmin customerAdmin=new CustomerAdmin();
                customerAdmin.setId (select.getInt ("id"));
                customerAdmin.setName (select.getString ("name"));
                customerAdmin.setStarRating (select.getString ("star_rating"));
                customerAdmin.setPhone (select.getString ("phone"));
                customerAdmin.setFollowThough (select.getString ("follow_through"));
                customerAdmin.setSaleUser (select.getString ("sale_user"));
                customerAdmin.setNoFollow (select.getInt ("no_follow"));
                customerAdmin.setStates (select.getString ("states"));
                customerAdmin.setType(select.getInt("type"));
                 customerAdminList.add(customerAdmin);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return customerAdminList;
    }
    @Override
    public List<IndexInfo> getBtn(int pid) {
        Object[]objects=new Object[1];
        objects[0]=pid;
        ResultSet resultSet = BaseDao.select("select *from resources where parentId=?", objects);
        List<IndexInfo>indexInfoList=new ArrayList<>();
        try {
            while (resultSet.next()){
                IndexInfo indexInfo=new IndexInfo();
                indexInfo.setId(resultSet.getInt("id"));
                indexInfo.setIcon(resultSet.getString("icon"));
                indexInfo.setTitle(resultSet.getString("name"));
                indexInfo.setHref( resultSet.getString("resUrl"));
                indexInfoList.add(indexInfo);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return indexInfoList;
    }

    @Override
    public List<CustomerAdmin> selectByName(String uname) {
        List<CustomerAdmin>customerAdminList=new ArrayList<>();
        Object[] objects=new Object[1];
        objects[0]=uname;
        ResultSet select = BaseDao.select ("select * from customer_admin where name=? ", objects);
        try {
            while (select.next ()){
                CustomerAdmin customerAdmin=new CustomerAdmin();
                customerAdmin.setId (select.getInt ("id"));
                customerAdmin.setName (select.getString ("name"));
                customerAdmin.setStarRating (select.getString ("star_rating"));
                customerAdmin.setPhone (select.getString ("phone"));
                customerAdmin.setFollowThough (select.getString ("follow_through"));
                customerAdmin.setSaleUser (select.getString ("sale_user"));
                customerAdmin.setNoFollow (select.getInt ("no_follow"));
                customerAdmin.setStates (select.getString ("states"));
                customerAdmin.setType(select.getInt("type"));
                customerAdminList.add(customerAdmin);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return customerAdminList;
    }


}
